<?php
namespace app\admin\api;

use app\admin\service\PermissionService;
use app\admin\validate\PermissionValidate;
use think\facade\Request;

class Permission extends Base
{
    /**
     * 初始化方法
     *
     * @param  \app\admin\service\PermissionService $permission
     */
    public function __construct(PermissionService $permission)
    {
        parent::__construct();

        $this->service = $permission;
    }

    /**
     * 接口--获取权限列表
     *
     * @return
     */
    public function getList()
    {
        app('request')->pageSize  = 100;
        $data = $this->service->getList();
        return $data;
    }

    /**
     * 接口--删除单条记录操作
     *
     * @return
     */
    public function delete()
    {
        $id = Request::param('id');

        $this->service->destroy($id);

        return ['code'=>0,'msg'=>'删除成功！'];
    }

    /**
     * 新增操作
     *
     */
    public function store(PermissionValidate $permissionValidate)
    {
        $param = Request::param();

        if(!$permissionValidate->check($param)){
            return ['code'=>1,'msg'=>$permissionValidate->getError()];
        }

        $result = $this->service->store($param)
            ? ['code'=>0,'msg'=>'新增成功！']
            : ['code'=>1,'msg'=>'新增失败！'];

       return $result;
    }

    public function read($id)
    {
        $info  = $this->service->getById($id);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$info];
    }

    /**
     * 更新操作
     *
     */
    public function update(PermissionValidate $permissionValidate)
    {
        $param = Request::param();

        if(!$permissionValidate->check($param)){
            return ['code'=>1,'msg'=>$permissionValidate->getError()];
        }

        $result = $this->service->update($param)
            ? ['code'=>0,'msg'=>'修改成功！']
            : ['code'=>1,'msg'=>'修改失败！'];

        return $result;
    }
}
